﻿app.controller('ProfileController', function ($scope, $rootScope, $routeParams, $compile, userRoomFactory, roomFactory, userFactory, friendFactory, notificationFactory) {

    init();

    function init() {
        $scope.routeId = 'profile';
        $scope.severName = mainFileAddress;
        $scope.roleByGroup = JSON.parse(localStorage.getItem("menu"));
        $scope.userName = $rootScope.userName;
        //count--------------
        notificationFactory.countAddFriend(localStorage.getItem("idkey")).success(function (data) {
            $scope.countAddFriend = data;
        });
        notificationFactory.countFromRoom(localStorage.getItem("idkey")).success(function (data) {
            $scope.countFromRoom = data;
        });
        notificationFactory.countNewMessage(localStorage.getItem("idkey")).success(function (data) {
            $scope.countNewMessage = data;
        });

        $scope.searchRoomList = {};
        $('#listRoom').hide();


        //load user hoặc friend
        //loadInfomation();
        $scope.profileShowTask = false;
        $scope.friendShowTask = false;
        $scope.isFriend = false;
        $scope.waitAccept = false;
        var friendPost = {
            UserId: $routeParams.userId,
            FriendId: localStorage.getItem("idkey")
        };
        $scope.profile = { Id: null, UserName: null, FullName: null, Email: null, PhoneNumber: null, Status: null, Block: null, GroupId: null, Avatar: null };
        if ($routeParams.userId != null && $routeParams.userId != localStorage.getItem("idkey")) {
            $scope.isMyProfile = false;
            userFactory.getUser($routeParams.userId).success(function (dataUser) {
                $scope.profile = dataUser;
            }).error(function () { });
            friendFactory.checkWaitingAccept(friendPost).success(function (data) {
                if (data == "false")
                    $scope.waitAccept = false;
                else {
                    $scope.waitAccept = true;
                }
            });
            friendFactory.getFriends($routeParams.userId).success(function (data) {
                $scope.friends = data;
                for (var i = 0; i < $scope.friends.length; i++)
                    if ($scope.friends[i].FriendId == localStorage.getItem("idkey"))
                        $scope.isFriend = true;
            });
            userRoomFactory.GetRoomByUser($routeParams.userId).success(function (data) {
                $scope.roomByUser = data;
            });
        } else {
            userRoomFactory.GetRoomByUser(localStorage.getItem("idkey")).success(function (data) {
                $scope.roomByUser = data;
            });
            userFactory.getUser(localStorage.getItem("idkey")).success(function (data) {
                $scope.profile = data;
                $scope.isMyProfile = true;
            });
            friendFactory.getFriends(localStorage.getItem("idkey")).success(function (data) {
                $scope.friends = data;
            });
        }
        
        $scope.addFriend = function () {
            friendFactory.postFriend(friendPost).success(function () {
                $scope.waitAccept = true;
            });
        };
        $scope.friendShowTaskClick = function () {
            $scope.friendShowTask = !$scope.friendShowTask;
        };
        $scope.profileShowTaskClick = function () {
            $scope.profileShowTask = !$scope.profileShowTask;
        };

        $scope.deleteFriend = function () {
            friendFactory.delFriend({ FriendId: $routeParams.userId, UserId: localStorage.getItem("idkey") });
            $scope.friendShowTask = !$scope.friendShowTask;
            $scope.isFriend = false;
            $scope.waitAccept = false;
        };
        var dataPost = {
            PageIndex: 1,
            PageSize: 9
        };
        $scope.hasNextPage = {};
        $scope.hasPreviousPage = {};
        $scope.totalPages = {};
        var newp;
        var dom;
        $scope.findFriend = function () {
            //$scope.load
            if ($('#findFriendInput').val() != "") {
                dataPost = {
                    PageIndex: 1,
                    PageSize: 9,
                    SearchString: $('#findFriendInput').val()
                };
                userFactory.searchUser(dataPost).success(function (data) {
                    $scope.searchFriendList = data.List;
                    $scope.hasNextPage = data.HasNextPage;
                    $scope.hasPreviousPage = data.HasPreviousPage;
                    $scope.totalPages = data.TotalPages;
                    firstPageAppend();
                });
            }
        };
        function firstPageAppend() {
            dom = $('#paging');
            dom.html("");
            dom.append('<span class="current-page">' + dataPost.PageIndex + '</span>');
            if ($scope.hasNextPage) {
                newp = dataPost.PageIndex + 1;
                dom.append('<span class="next-page" ng-click=paging(' + newp + ')>›</span>');
            } else {
                if (newp != $scope.totalPages)
                    dom.append('<span class="last-page" ng-click=paging(' + $scope.totalPages + ')>»</span>');
            }
            var compiled = $compile(dom);
            compiled($scope);
        }
        function appendPage() {
                if (newp != 1)
                    dom.append('<span class="first-page" ng-click=paging(1)>«</span>');
                if ($scope.hasPreviousPage) {
                    dom.append('<span class="prev-page" ng-click=paging(' + (dataPost.PageIndex - 1) + ')>‹</span>');

                }
                dom.append('<span class="current-page">' + dataPost.PageIndex + '</span>');
                if ($scope.hasNextPage) {
                    dom.append('<span class="next-page" ng-click=paging(' + (dataPost.PageIndex + 1) + ')>›</span>');
                }
                if (newp != $scope.totalPages)
                    dom.append('<span class="last-page" ng-click=paging(' + $scope.totalPages + ')>»</span>');

                var compiled = $compile(dom);
                compiled($scope);

            }
        $scope.paging = function (newpage) {
            dataPost.PageIndex = newpage;
            dom = $('#paging');
            dom.empty();
            userFactory.searchUser(dataPost).success(function (data) {
                $scope.searchFriendList = data.List;
                $scope.hasNextPage = data.HasNextPage;
                $scope.hasPreviousPage = data.HasPreviousPage;
                appendPage();
            });
        };





        $('#listRoom').fadeIn(500);
        $scope.deleteFriendHasId = function (friend) {
            var result = confirm("Xóa " + friend.FriendName + " ra khỏi danh sách bạn bè?");
            if (result == true) {
                friendFactory.delFriendHasId(friend.Id);
                $("#friend_" + friend.Id).fadeOut(500);
            }
        };


        createMenu();
    }
});